from exit import db
from datetime import datetime, date


class Areas(db.Model):  # 地址区域数据
    __tablename__ = 'c_areas'
    id = db.Column(db.String(100), primary_key=True)    # id
    name = db.Column(db.String(255))  # 名字
    parent_id = db.Column(db.String(100), index=True)   # 父id


class TradeManage(db.Model):      # 交易管理
    __tablename__ = 'c_trade_manage'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    start_time = db.Column(db.Time)
    end_time = db.Column(db.Time)
    trade_type = db.Column(db.Integer)
    trade_name = db.Column(db.String(100))
    trade_status = db.Column(db.Integer, default=0)


class GlobalParams(db.Model):   # 全局参数
    __tablename__ = 'c_global_params'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    global_params = db.Column(db.DECIMAL(20, 4))     # 价格
    g_type = db.Column(db.Integer, default=0)    # 类型
    g_name = db.Column(db.String(64))    # 类型
    params_add = db.Column(db.DECIMAL(20, 4))    # 增长

    @classmethod
    def get_params(cls, g_type):
        params = GlobalParams.query.filter_by(g_type=g_type).first()
        return params.global_params


class PayAccount(db.Model):     # 支付账户
    __tablename__ = 'c_pay_account'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    pay_name = db.Column(db.String(255))
    pay_img = db.Column(db.String(255))
    pay_card = db.Column(db.String(50))
    real_name = db.Column(db.String(50))
    bank_name = db.Column(db.String(16))
    pay_type = db.Column(db.Integer)

    def pay_account_json(self):
        return {
            'pay_name': self.pay_name,
            'pay_img': self.pay_img,
            'pay_card': self.pay_card,
            'real_name': self.real_name,
            'bank_name': self.bank_name,
        }


class WithdrawOrder(db.Model):  # 提现订单
    __tablename__ = 'c_order_withdraw'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32))
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    status = db.Column(db.Integer, default=0)
    price = db.Column(db.DECIMAL(20, 3), default=0)
    income = db.Column(db.DECIMAL(20, 3), default=0)
    num = db.Column(db.Integer, default=0)
    trade_no = db.Column(db.String(100))
    withdraw_img = db.Column(db.String(255))    # 提现图片
    w_type = db.Column(db.Integer)      # 钱包类型
    withdraw_date = db.Column(db.Date)
    pay_type = db.Column(db.Integer)    # 提现类型
    to_status = db.Column(db.Integer, default=0)    # 提现类型

    def withdraw_json(self):
        if self.update_time is not None:
            self.update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'id': self.id,
            'user_id': self.user_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time,
            'status': self.status,
            'price': self.price,
            'income': self.income,
            'w_type': self.w_type,
            'pay_type': self.pay_type,
            'withdraw_img': self.withdraw_img,
            'to_status': self.to_status,
        }


class RechargeOrder(db.Model):  # 充值订单
    __tablename__ = 'c_order_recharge'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32), index=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    status = db.Column(db.Integer, default=0, index=True)
    price = db.Column(db.DECIMAL(20, 2), default=0)
    number = db.Column(db.Integer, default=0)
    trade_no = db.Column(db.String(32))
    order_img = db.Column(db.String(255))
    w_type = db.Column(db.Integer, index=True)      # 0会员订单 1云积分 2ds

    def recharge_order_json(self):
        if self.update_time is not None:
            self.update_time = self.update_time.strftime('%Y-%m-%d %H:%M:%S')
        return {
            'id': self.id,
            'user_id': self.user_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time,
            'status': self.status,
            'price': self.price,
            'number': self.number,
            'w_type': self.w_type,
            'trade_no': self.trade_no,
            'order_img': self.order_img,
        }


class Article(db.Model):  # 文章
    __tablename__ = 's_article'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # user_id = db.Column(db.String(32), index=True)
    # type_id = db.Column(db.Integer, db.ForeignKey('s_article_type.id'), index=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    article_time = db.Column(db.Date)
    status = db.Column(db.Integer, default=0, index=True)
    title = db.Column(db.String(1024))  # 标题
    content = db.Column(db.Text)    # 内容
    article_img = db.Column(db.String(1024))    # 图片
    author = db.Column(db.String(255))     # 绑定作家
    # works = db.Column(db.String(2048), index=True)     # 绑定作品

    # article_type = db.relationship('ArticleType')

    def article_json(self):
        return {
            'article_id': self.id,
            # 'type_id': self.type_id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'article_time': str(self.article_time),
            'update_time': self.update_time,
            'status': self.status,
            'title': self.title,
            'content': self.content,
            'article_img': self.article_img,
            'author': self.author,
        }
